package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.mapper.StatisticsMapper;
import com.jiyun.order.mapper.OrdersMapper;
import com.jiyun.orders.pojo.Orders;
import com.jiyun.service.StatisticsService;
import com.jiyun.statistics.ChartDataVo;
import com.jiyun.statistics.Count;
import com.jiyun.xxl.client.XxlJobClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zqtao
 */

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public Page<Orders> getData(Integer pageNum, Integer pageSize) {
        Page<Orders> page = new Page<>(pageNum, pageSize);
        Page<Orders> data = statisticsMapper.getData(page);
        return data;
    }

    @Autowired
    private XxlJobClient xxlJobClient;

    @Override
    public Long startXxlJob(Integer pageNum, Integer pageSize) {
        String params = "pageNum=" + pageNum + "&pageSize=" + pageSize;
        Long jobId = xxlJobClient.addAndStart("newOrderTaskHandler", params, "0 0/1 * * * ?", "新的任务：pageNum为：" + pageNum + "pageSize为" + pageSize);
        return jobId;
    }

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public Count countToday() {

        // 获取当天00:00:00
        LocalDateTime startTime = LocalDateTime.now().with(LocalTime.MIN);
        // 获取当天23:59:59
        LocalDateTime endTime = LocalDateTime.now().with(LocalTime.MAX);

        Count count = getCountDate(startTime, endTime);


        // 方式1：使用Java 8+的java.time.LocalDate（推荐）
        LocalDate today = LocalDate.now();
        LocalDate lastMonthSameDay = today.minusMonths(1);
        LocalDateTime start = lastMonthSameDay.atTime(LocalTime.MIN);
        LocalDateTime end = lastMonthSameDay.atTime(LocalTime.MAX);
        Count count2 = getCountDate(start, end);

        //计算涨幅
        if(count2.getCountSuccess() != 0){
            double result = (count.getCountSuccess() - count2.getCountSuccess()) * 1.0 / count2.getCountSuccess();
            Integer i = (int) Math.round(result);
            count.setCountSuccessGrowth(i);
        }else{
            count.setCountSuccessGrowth(0);
        }
        if(count2.getCountAllocate() != 0){
            double result = (count.getCountAllocate() - count2.getCountAllocate()) * 1.0 / count2.getCountAllocate();
            Integer i = (int) Math.round(result);
            count.setCountAllocateGrowth(i);
        }else{
            count.setCountAllocateGrowth(0);
        }
        if(count2.getCountTransportIng() != 0){
            double result = (count.getCountTransportIng() - count2.getCountTransportIng()) * 1.0 / count2.getCountTransportIng();
            Integer i = (int) Math.round(result);
            count.setCountTransportIngGrowth(i);
        }else{
            count.setCountTransportIngGrowth(0);
        }
        if(count2.getCountComplete() != 0){
            double result = (count.getCountComplete() - count2.getCountComplete()) * 1.0 / count2.getCountComplete();
            Integer i = (int) Math.round(result);
            count.setCountCompleteGrowth(i);
        }else{
            count.setCountCompleteGrowth(0);
        }

        return count;
    }

    @Override
    public Count countMouth() {

        return null;
    }

    @Override
    public ChartDataVo getChartData() {

        ChartDataVo chartDataVo = new ChartDataVo();

        // 获取最近7天的日期列表
        List<String> dateList = new ArrayList<>();
        List<Integer> orderCountList = new ArrayList<>();

        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 循环获取最近7天的数据
        for(int i = 6; i >= 0; i--) {
            LocalDate date = today.minusDays(i);
            String dateStr = date.format(DateTimeFormatter.ofPattern("MM-dd"));
            dateList.add(dateStr);

            // 获取每天的订单总数
            Integer orderCount = ordersMapper.getOrderCountByDate(date);
            orderCountList.add(orderCount);

        }

        // 设置返回数据
        chartDataVo.setDates(dateList);
        chartDataVo.setValues(orderCountList);

        return chartDataVo;
    }

    private Count getCountDate(LocalDateTime startTime, LocalDateTime endTime) {

        Count count = new Count();


        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getOrderStatus, 1);
        wrapper.between(Orders::getOrderTime, startTime, endTime);
        List<Orders> orders = ordersMapper.selectList(wrapper);
        count.setCountSuccess(orders.size());

        LambdaQueryWrapper<Orders> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Orders::getOrderStatus, 2);
        wrapper1.between(Orders::getOrderTime, startTime, endTime);
        List<Orders> orders1 = ordersMapper.selectList(wrapper1);
        count.setCountAllocate(orders1.size());

        LambdaQueryWrapper<Orders> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Orders::getOrderStatus, 3);
        wrapper2.between(Orders::getOrderTime, startTime, endTime);
        List<Orders> orders2 = ordersMapper.selectList(wrapper2);
        count.setCountTransportIng(orders2.size());

        LambdaQueryWrapper<Orders> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(Orders::getOrderStatus, 4);
        wrapper3.between(Orders::getOrderTime, startTime, endTime);
        List<Orders> orders3 = ordersMapper.selectList(wrapper3);
        count.setCountComplete(orders3.size());

        return count;

    }






}
